//
//  TCLYTieZiViewController.m
//  YuErmuYing
//
//  Created by LYang on 16/8/19.
//  Copyright © 2016年 Tiancheng. All rights reserved.
//

#import "TCLYTieZiViewController.h"
#import "TCLYEditorTieZiViewController.h"
#import "Masonry.h"
#import "Macros.h"
#import "TCLYMineModel.h"
#import "TCLYTieZiTableViewCell.h"
#import "UIView+HB.h"
#import "UIImageView+WebCache.h"
#import "TCLYRegisterView.h"
#import "TCLYUser.h"

@interface TCLYTieZiViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView * tieziTableView;
@property (nonatomic, strong) TCLYMineModel * mineModel;
@property (nonatomic, strong) TCLYRegisterView * registerView;
@property (nonatomic, strong) TCLYUser * user;
@end

@implementation TCLYTieZiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.barStyle = UIBarMetricsCompactPrompt;
    self.navigationController.navigationBar.backgroundColor = statusColor;
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, -20, screenWidth, 20)];
    view.backgroundColor = navColor;
    [self.navigationController.navigationBar addSubview:view];
    
    UILabel * titleLabel = [[UILabel alloc] init];
    titleLabel.text = @"帖子";
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.frame = CGRectMake(0, 0, 50, 20);
    titleLabel.font = [UIFont boldSystemFontOfSize:20];
    self.navigationItem.titleView = titleLabel;
    
    UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(12, 22, 20, 20);
    [leftBtn setImage:[UIImage imageNamed:@"left"] forState:UIControlStateNormal];
    leftBtn.tag = 91;
    [leftBtn addTarget:self action:@selector(statusBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(createWork) name:@"loadTieZiSucceed" object:nil];
    self.mineModel = [[TCLYMineModel alloc] init];
    self.user = [[TCLYUser alloc] init];
    NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:self.user.userId, @"uid", nil];
    [self.mineModel loadTieZiParameters:dic];
    
    self.registerView = [[TCLYRegisterView alloc] init];
    // Do any additional setup after loading the view.
    [self createView];
}
-(void)createWork{
    NSLog(@"收到通知");
    [self.tieziTableView reloadData];
}
-(void)createView{
    self.tieziTableView = [[UITableView alloc] init];
    self.tieziTableView.backgroundColor = bgColor;
    [self.tieziTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    self.tieziTableView.dataSource = self;
    self.tieziTableView.delegate = self;
    [self.view addSubview:self.tieziTableView];
    [self.tieziTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view);
        make.left.mas_equalTo(self.view);
        make.bottom.mas_equalTo(self.view);
        make.right.mas_equalTo(self.view);
    }];
}
//tabelView必须实现的代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    NSLog(@"数组长度%lu",(unsigned long)self.mineModel.TieZiArr.count);
//    NSLog(@"数组数据%@",self.mineModel.TieZiArr[0]);
    return self.mineModel.TieZiArr.count;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * identifier = @"tieZiCell";
    TCLYTieZiTableViewCell * cell = [[TCLYTieZiTableViewCell alloc] initWithReuseIdentifier:identifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSURL * url = [NSURL URLWithString:self.mineModel.TieZiArr[indexPath.row][@"upic"]];
    [cell.headerImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"LYJia2"]];
    cell.nickNameLabel.text = self.mineModel.TieZiArr[indexPath.row][@"username"];
    cell.titleLabel.text = self.mineModel.TieZiArr[indexPath.row][@"notename"];
    CGSize titleSize = [cell.titleLabel sizeThatFits:CGSizeMake(cell.titleLabel.bounds.size.width, MAXFLOAT)];
    [cell.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(cell.mas_left).offset(6);
        make.top.mas_equalTo(cell.nickNameLabel.mas_bottom).offset(6);
        make.right.mas_equalTo(cell.mas_right).offset(-6);
        make.height.mas_equalTo(titleSize.height);
    }];
    cell.contentLabel.text = self.mineModel.TieZiArr[indexPath.row][@"describle"];
    CGSize contentSize = [cell.contentLabel sizeThatFits:CGSizeMake(cell.contentLabel.bounds.size.width, MAXFLOAT)];
    [cell.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(cell.mas_left).offset(6);
        make.top.mas_equalTo(cell.titleLabel.mas_bottom).offset(6);
        make.right.mas_equalTo(cell.mas_right).offset(-6);
        
        make.height.mas_equalTo(contentSize.height);
    }];
    cell.timeLabel.text = self.mineModel.TieZiArr[indexPath.row][@"time"];
    [cell.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(cell.contentLabel);
        make.top.mas_equalTo(cell.contentLabel.mas_bottom).offset(6);
        make.right.mas_equalTo(cell.mas_right).offset(-6);
        make.height.mas_equalTo(20);
    }];
    cell.agreedLabel.text = [NSString stringWithFormat:@"%@赞同",self.mineModel.TieZiArr[indexPath.row][@"look"]];
    
    cell.commentsLabel.text = [NSString stringWithFormat:@"%@评价",self.mineModel.TieZiArr[indexPath.row][@"comment"]];
    
    
    [cell.editorBtn addTarget:self action:@selector(editorBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    cell.editorBtn.tag = 1000+indexPath.row;
    [cell.deleteBtn addTarget:self action:@selector(deleteBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    cell.deleteBtn.tag = 3000+indexPath.row;
    double height = titleSize.height + contentSize.height +20+12+20+30+6+6+6+20;
    cell.height = height;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    
    return cell.frame.size.height;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//编辑帖子
-(void)editorBtnAction:(id)sender{
    TCLYEditorTieZiViewController * editorTieZiViewCtr = [[TCLYEditorTieZiViewController alloc] init];
    editorTieZiViewCtr.editorTieZiDic = self.mineModel.TieZiArr[((UIButton *)sender).tag-1000];
    [self.navigationController pushViewController:editorTieZiViewCtr animated:YES];
    
}
//删除帖子
-(void)deleteBtnAction:(id)sender{
    NSLog(@"删除按钮");
    NSLog(@"%@",self.mineModel.TieZiArr[((UIButton *)sender).tag-3000][@"nid"]);
    [self.registerView Prompt:@"您确定要删除此帖子吗?" Controller:self];
    self.registerView.determineAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deleteBtnAction) name:@"deleteTieZiSucced" object:nil];
        
        NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:self.mineModel.TieZiArr[((UIButton *)sender).tag-3000][@"nid"],@"nid",self.user.userId,@"uid", nil];
        [self.mineModel deleteTieZiParameters:dic];
        return ;
    }];
    self.registerView.cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"quxiao");
    }];
        [self.registerView.promptAlertCtr addAction:self.registerView.determineAction];
        [self.registerView.promptAlertCtr addAction:self.registerView.cancelAction];

}
-(void)statusBtnAction:(id)sender{
    switch (((UIButton *) sender).tag) {
        case 91:{
            self.navigationController.navigationBarHidden = YES;
            self.tabBarController.tabBar.hidden = NO;
            [self.navigationController popViewControllerAnimated:YES];
            
        }
            break;
    }
}
-(void)deleteBtnAction{
    NSLog(@"删除成功");
    [self.tieziTableView reloadData];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
